package com.yx.sys.controller;

import ch.qos.logback.core.util.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.common.vo.Result;
import com.yx.sys.entity.Post;
import com.yx.sys.entity.User;
import com.yx.sys.entity.Wiki;
import com.yx.sys.service.IPostService;
import com.yx.sys.service.IUserService;
import com.yx.sys.service.IWikiService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.parser.Entity;
import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 提问或推荐 前端控制器
 * </p>
 *
 * @author yx
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private IPostService iPostService;

    @Autowired
    private IWikiService wikiService;

    @GetMapping("/all")
    public Result<List<Post>> getAllPost(){
        List<Post> list = iPostService.list();
        return Result.success(list,"查询所有post信息成功");
    }
    //获取排行榜10条
    @GetMapping("/hot")
    public Result<List<Post>> selectHotPosts(Integer num){
//        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByDesc(Post::getViewNums,Post::getReplyNums).last("limit 10");
//        List<Post> list = iPostService.list(wrapper);
//        System.out.println(list);
        List<Post> list = iPostService.getHotPosts(num);
        return Result.success(list,"查询热门post信息成功");
    }
//    获取两篇最新发布的信息
    @GetMapping("/new")
    public Result<List<Post>> selectNewPosts(Integer num){
//        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByDesc(Post::getpTime).last("limit "+ num);
//        List<Post> list = iPostService.list(wrapper);
//        System.out.println(list);
        List<Post> list = iPostService.getNewPosts(num);
        return Result.success(list,"查询最新发布的信息成功");
    }
    //    获取两篇热门发布的信息
    @GetMapping("/hotMes")
    public Result<List<Post>> selectHotPostsBytId(Integer tId,Integer num){
        System.out.println(tId);
        System.out.println(num);
        List<Post> list = iPostService.getHotPostsBytId(tId,num);
        return Result.success(list,"查询最新发布的信息成功");
    }


//    根据标签id获取post信息
    @GetMapping("/{tId}")
    public Result<List<Post>> selectPostBytId(@PathVariable("tId") Integer tId){
        List<Post> posts = iPostService.getPostMesBytId(tId);
        return Result.success(posts,"查询所有post信息成功");
    }

//    更新：查看数+1
//    根据文章id使文章阅读数加1
    @PutMapping("/view")
    public Result<Post> updatePostView(@RequestBody Post params){
        System.out.println(params);
//        System.out.println(params.getpId());
        LambdaUpdateWrapper<Post> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Post::getpId,params.getpId()).set(Post::getViewNums,params.getViewNums()+1);
        iPostService.update(updateWrapper);
        Post post = iPostService.getById(params.getpId());
        return Result.success(post,"更新查看数成功");
    }

//    发布信息
    @PostMapping
    public Result<?> postMes(@RequestBody Map<String,Object> params){
        System.out.println(params);
        Map postForm = (Map)params.get("postForm");
        Integer wId = null;
        if((Boolean) postForm.get("isLink")){
            System.out.println("****************************************");
            Map wikiForm = (Map)params.get("wikiForm");
            wikiForm.put("tId",postForm.get("tId"));
            //转换对象
            Wiki wikiForm1 = JSONObject.parseObject(JSONObject.toJSONString(wikiForm)).toJavaObject(Wiki.class);
            System.out.println(wikiForm);
            wId = wikiService.findWikiIsExist(wikiForm1.getwName(),wikiForm1.getwLink(), wikiForm1.gettId(),wikiForm1.geteId());
            System.out.println("wId:"+wId);
            if(wId==null){
                wikiService.save(wikiForm1);
                wId = wikiService.findWikiIsExist(wikiForm1.getwName(),wikiForm1.getwLink(), wikiForm1.gettId(),wikiForm1.geteId());
            }

            postForm.put("wId",wId);
        }
        System.out.println("--------------------------------");
        Post postForm2 = JSONObject.parseObject(JSONObject.toJSONString(postForm)).toJavaObject(Post.class);
        Integer pId = iPostService.getPostIsExist(postForm2.getTitleName(), postForm2.getpImg(), postForm2.getpDescribe(),  postForm2.gettId(), postForm2.getuId());
//        System.out.println("postForm:"+postForm);
        System.out.println(pId);
        if(pId==null){
            iPostService.save(postForm2);
            return Result.success("添加成功");
        }else{
            return Result.fail("该记录已经存在，请勿重复提交！");
        }
//        return null;
    }

//    根据post标题模糊查询
//    标题 图片 标签 观看数 内容 作者 评论数
    @GetMapping("fuzzyTitle")
    public Result<?> selectFuzzyTitle(@RequestParam("titleName") String titleName){
        List<?> list = iPostService.getFuzzyTitle(titleName);
        return Result.success(list);
    }
    @GetMapping
    public Result<?> selectTitle(){
        return null;
    }

//    根据用户id查询post文章
//    @GetMapping("/{uId}")
//    public Result<List<Post>> selectPostByuId(@PathVariable("uId") Integer uId){
//        List<Post> posts = iPostService.getPostMesByuId(uId);
//        return Result.success(posts,"查询所有post信息成功");
//    }
//    根据uId获取用户发布文章信息
    @GetMapping("/postMes")
    public Result<Map<String,Object>> selectPostMesByuId(
            @RequestParam(value ="uId",required = false) Integer uId,
            @RequestParam(value ="status",required = false) Integer status,
//            @RequestParam(value = "pageNo",required = false) Integer pageNo,
//            @RequestParam(value = "pageSize",required = false) Integer pageSize,
            @RequestParam(value ="tag",required = false) Integer tag){
        // status -1       1     2     3
        //       草稿    进行中  通过 未通过
//        System.out.println(status);
//        System.out.println(uId);
//        System.out.println(pageNo);
//        System.out.println(pageSize);
//        System.out.println(tag);
//        Integer startId = null;
//        if(pageNo!=null){
//            startId = pageSize*(pageNo-1);
//            System.out.println(startId);
//        }
//        List<Post> postMes = iPostService.getMesByuId(status,uId,startId,pageSize,tag);
        List<Post> postMes = iPostService.getMesByuId(status,uId,tag);

        Map<String, Object> data = new HashMap<>();
//        if(postMes.size()>0){
//            data.put("selectTotal",iPostService.list().toArray().length);
//        }else{
//            data.put("selectTotal",postMes.size());
//        }
//        System.out.println(postMes.size());
//        System.out.println(iPostService.list().toArray().length);
//        data.put("total",iPostService.list().toArray().length);
//        data.put("selectTotal",postMes.size());
        data.put("rows",postMes);
//        System.out.println(data);
        return Result.success(data,"获取post信息成功");
    }


//    回复数+1
    @PutMapping("addFansNum")
    public void addFansNum(@RequestParam("pId") Integer pId){
        System.out.println("pId:"+pId);
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        wrapper.eq("pId",pId);
        Post post = iPostService.getOne(wrapper);
        System.out.println(post);
//        Integer replyNums = iPostService.getById(6).getReplyNums();
//        System.out.println("replyNums:"+replyNums);
//        LambdaUpdateWrapper<Post> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.eq(Post::getpId,pId).set(Post::getReplyNums,replyNums+1);
//        iPostService.update(updateWrapper);
    }

//    根据post id更改文章状态
    @PutMapping("updateUpload")
    public Result<Boolean> updateUpload(@RequestParam("pId") Integer pId,
                             @RequestParam("num") Integer num){
        System.out.println("pId:"+pId);
        LambdaUpdateWrapper<Post> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Post::getpId,pId);
        Post post = iPostService.getOne(wrapper);
        System.out.println(post);
        boolean update = true;
        if(post!=null){
            wrapper.set(Post::getIsUpload,num);
            update = iPostService.update(wrapper);
        }
        return Result.success(update,"修改文章状态成功");
    }

//    根据pId查询信息
    @GetMapping("/postMesBypId")
    public Result<?> getPostBypId(Integer pId){
        List<Post> list = iPostService.getPostBypId(pId);
        return Result.success(list);
    }
}


